package logic

import (
	"api/internal/svc"
	"api/internal/types"
	"api/model/order"
	"api/service"
	"context"
	"github.com/zeromicro/go-zero/core/stores/sqlx"

	"github.com/zeromicro/go-zero/core/logx"
)

type OrderFoodOrderUpdateLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewOrderFoodOrderUpdateLogic(ctx context.Context, svcCtx *svc.ServiceContext) *OrderFoodOrderUpdateLogic {
	return &OrderFoodOrderUpdateLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

// 商家修改订单状态 仅允许
func (l *OrderFoodOrderUpdateLogic) OrderFoodOrderUpdate(req *types.RequestOrderFoodOrderUpdate) (resp *types.ResponseMsg, err error) {
	if req.Status != 4 && req.Status != 5 {
		return service.Return400()
	}

	conn := sqlx.NewMysql(l.svcCtx.Config.DataSource)
	orderModel := order.NewOrderModel(conn)

	busService := service.NewBusinessService(l.ctx, l.svcCtx)
	bus := busService.GetLoginBusinessInfo()

	err = orderModel.UpdateStatus(l.ctx, &order.Order{
		BusinessId:   int(bus.Id),
		Status:       req.Status,
		InnerTradeNo: req.InnerTradeNo,
	})

	if err != nil {
		return service.ReturnError()
	}

	return service.ReturnOk()
}
